import { PoolManager } from "./Framework/Managers/PoolManager";
import NodeUtil from "./Framework/Utils/NodeUtil";
import GrenadeListener from "./GrenadeListener";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Grenade extends cc.Component {

    rigidbody: cc.RigidBody = null;

    Sp: cc.Sprite = null;

    Boom: cc.Animation = null;

    grenadeListener: GrenadeListener = null;

    onLoad() {
        this.Sp = NodeUtil.GetComponent("Sp", this.node, cc.Sprite);
        this.Boom = NodeUtil.GetComponent("Boom", this.node, cc.Animation);
        this.rigidbody = this.node.getComponent(cc.RigidBody);
        this.grenadeListener = this.Sp.getComponent(GrenadeListener);
    }

    protected onEnable(): void {
        this.Boom.on('finished', this.Put, this);
    }

    protected onDisable(): void {
        this.Boom?.off('finished', this.Put, this);
    }

    Put() {
        this.scheduleOnce(() => {
            PoolManager.PutNode(this.node);
        });
    }

    Throw(dir: cc.Vec2) {
        this.rigidbody.applyLinearImpulse(dir.multiplyScalar(2000), cc.Vec2.ZERO, true);

        this.scheduleOnce(() => {
            this.Boom.play();
        }, 3);
    }
}